﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Simpletron
{
  public interface IMemoryDumpWriter
  {
    void WriteDump(string dump);
    void Flush();
    void Close();
  }

  public class MemoryDumpConsoleWriter : IMemoryDumpWriter
  {
    #region IMemoryDumpWriter Members

    public void WriteDump(string dump)
    {
      Console.WriteLine(dump);
    }

    public void Flush()
    { }

    public void Close()
    { }


    #endregion
  }

  public class MemoryDumpFileWriter : IMemoryDumpWriter
  {
    private StreamWriter writer;

    public MemoryDumpFileWriter()
    {
      writer = new StreamWriter("brainfuck.dump");
    }

    #region IMemoryDumpWriter Members

    public void WriteDump(string dump)
    {
      writer.WriteLine(dump);
    }

    public void Flush()
    {
      writer.Flush();
    }

    public void Close()
    {
      writer.Close();
    }

    #endregion
  }

  public class MemoryDumpCompositeWriter : IMemoryDumpWriter
  {
    private List<IMemoryDumpWriter> writers;

    public MemoryDumpCompositeWriter()
    {
      writers = new List<IMemoryDumpWriter>();
    }

    public MemoryDumpCompositeWriter Add(IMemoryDumpWriter writer)
    {
      writers.Add(writer);
      return this;
    }

    public static IMemoryDumpWriter CreateConsoleAndFileWriter()
    {
      return new MemoryDumpCompositeWriter()
        .Add(new MemoryDumpConsoleWriter())
        .Add(new MemoryDumpCompositeWriter());
    }

    #region IMemoryDumpWriter Members

    public void WriteDump(string dump)
    {
      writers.ForEach(i => i.WriteDump(dump));
    }

    public void Flush()
    {
      writers.ForEach(i => i.Flush());
    }

    public void Close()
    {
      writers.ForEach(i => i.Close());
    }

    #endregion
  }


}
